package com.gold.gauss.service;

import com.gold.gauss.dao.CatalogDao;
import com.gold.gauss.model.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created by Gary on 2019-12-24.
 *
 * @author Gary
 * @version 0.0.1
 * @date 2019-12-24
 * @since 0.0.1
 */
@Service
public class CatalogService {
    @Autowired
    private CatalogDao catalogDao;
    @Autowired
    private StackItemService stackItemService;

    public int create(Catalog catalog) {
        return catalogDao.create(catalog);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {

        try {
            catalogDao.delete(id);
            stackItemService.deleteByBookId(id);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
